package sms.testsuite;



import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import module.ExtendTestSuite;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;

import module.ExtendTestSuite.TestResult;
import sms.automation.common.Constant;
import until.Reporter;

public class Login extends ExtendTestSuite {
	

	
	
    public Reporter report;
    public String baseURL;
    public WebDriver driver;
    
    public static WebElement txtusername;
    public static WebElement txtpassword;
    public static WebElement loginbutt;
    public static WebElement registerlink;
    public static WebElement forgetPasslink;
    public static WebElement logo;
    
    public static String funtionname="Login Funtion";
    
    public  String userName;
    public  String password;
    public String note;
  
    
  @BeforeTest
  public void beforeTest() {
	initDriver();
	driver=getDriver();
	baseURL=testconfig.readNode("urlbase");
	
	  
	driver.get(baseURL);
	
  }
  

  @Test(priority=1)
  public void Login_UserAccount_NS1_Check_UI(){
	  
	  String casename=getClass().getName();
	  txtusername=findElementSystemAdmin("login|username");
	  txtpassword=findElementSystemAdmin("login|password");
	  loginbutt=findElementSystemAdmin("login|loginbutt");
	  registerlink=findElementSystemAdmin("login|linkregister");
	  forgetPasslink=findElementSystemAdmin("login|linkforgetpass");
	  logo=findElementSystemAdmin("login|logo");
	  
		if(txtusername==null || txtpassword==null || loginbutt==null|| registerlink==null || forgetPasslink==null || logo==null)
		{
			note="Miss anything view!!";
			String reportFile=Constant.REPORT_PATH +Constant.reportName;
			report=new Reporter(reportFile);
			report.addReport(funtionname, "Login_UserAccount_NS1", "All User role",note,TestResult.Fail);
		}
		
		
		else
		{
			note="";
		String reportFile=Constant.REPORT_PATH +Constant.reportName;
		report=new Reporter(reportFile);
		report.addReport(funtionname,casename, "All User role","",TestResult.Pass);
		}
			
  }
  
  @Test(priority=2)
  public void Login_UserAccount_NS1_System_Admin(){
	   
	  try {
	    	 userName=findElementSystemAdmin("account|username").toString();
			 password=findElementSystemAdmin("account|password").toString();
		 
		   
		     txtusername.clear();
		  	 txtusername.sendKeys(userName);
			 
		  	 txtpassword.clear();
		  	 txtpassword.sendKeys(password);
			
			loginbutt.click();
			findElementSystemAdmin("menu|showmenu").click();
			findElementSystemAdmin("menu|logout").click();
			 
		    
			findElementSystemAdmin("menu|logoutconfirm").click();
			
			String reportFile=Constant.REPORT_PATH +Constant.reportName;
			report=new Reporter(reportFile);
			report.addReport("FuntionLogin", "Login_UserAccount_NS1", "System_Admin","",TestResult.Pass);
		} 
	  catch (Exception e) {
			// TODO: handle exception
			String reportFile=Constant.REPORT_PATH +Constant.reportName;
			report=new Reporter(reportFile);
			report.addReport("FuntionLogin", "Login_UserAccount_NS1", "System_Admin","",TestResult.Fail);
		}
	    
		
		
	}
		
        
  @Test(priority=3)
  public void Login_UserAccount_NS1_Admin(){
	  
	  driver.get("http://localhost:5000/Healthcare/client/");
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

	    driver.findElement(By.id("ext-element-36")).clear();
		driver.findElement(By.id("ext-element-36")).sendKeys("admin@a.com");
		
		driver.findElement(By.id("ext-element-42")).clear();
		driver.findElement(By.id("ext-element-42")).sendKeys("sms");
		driver.findElement(By.xpath("//*[@id='ext-element-53']")).click();
		
		driver.findElement(By.id("btnHideShow")).click();
		driver.findElement(By.id("ext-element-137")).click();
		driver.findElement(By.id("ext-button-8")).click();
		
		String reportFile=Constant.REPORT_PATH +Constant.reportName;
		report=new Reporter(reportFile);
		report.addReport("FuntionLogin", "Login_UserAccount_NS1", "Admin","",TestResult.Pass);
		
		
  }

  @Test(priority=4)
  public void Login_UserAccount_NS1_Therapist(){
	  
	  driver.get("http://localhost:5000/Healthcare/client/");
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

	    driver.findElement(By.id("ext-element-36")).clear();
		driver.findElement(By.id("ext-element-36")).sendKeys("longvuduc02@gmail.com");
		
		driver.findElement(By.id("ext-element-42")).clear();
		driver.findElement(By.id("ext-element-42")).sendKeys("123456789");
		driver.findElement(By.xpath("//*[@id='ext-element-53']")).click();
		driver.findElement(By.id("ext-element-49")).clear();
		driver.findElement(By.id("ext-element-49")).sendKeys("Health Care");
		//Find dropdown list element
	if(driver.findElement(By.id("ext-element-49"))!=null){
         driver.findElement(By.id("ext-element-49")).click();
         driver.findElement(By.id("ext-element-111")).click();
         driver.findElement(By.id("ext-button-1")).click();
 	
	}
		driver.findElement(By.id("btnHideShow")).click();
		driver.findElement(By.id("ext-element-167")).click();
		driver.findElement(By.id("ext-button-8")).click();
		
		String reportFile=Constant.REPORT_PATH +Constant.reportName;
		report=new Reporter(reportFile);
		report.addReport("FuntionLogin", "Login_UserAccount_NS1", "Therapist","",TestResult.Pass);
  }
  @Test(priority=5)
  public void Login_UserAccount_NS1_Patient(){
	  
	  driver.get("http://localhost:5000/Healthcare/client/");
	  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 
	    driver.findElement(By.id("ext-element-36")).clear();
		driver.findElement(By.id("ext-element-36")).sendKeys("longvd@student.ptithcm.edu.vn");
		driver.findElement(By.xpath("//*[@id='ext-element-53']")).click();
		//Find dropdown list element
		if(driver.findElement(By.id("ext-element-49"))!=null){
	         driver.findElement(By.id("ext-element-49")).click();
	         driver.findElement(By.id("ext-element-105")).click();
	         driver.findElement(By.id("ext-button-1")).click();
	 	
		}
		if(driver.findElement(By.id("ext-sheet-1"))!=null){
			driver.findElement(By.id("ext-button-7")).click();
			
		}
		driver.findElement(By.id("btnHideShow")).click();
		driver.findElement(By.id("ext-element-152")).click();
		driver.findElement(By.id("ext-button-9")).click();
	
		
		String reportFile=Constant.REPORT_PATH +Constant.reportName;
		report=new Reporter(reportFile);
		report.addReport("FuntionLogin", "Login_UserAccount_NS1", "Patient","",TestResult.Pass);
  }
  @AfterTest
  public void afterTest() {
	  driver.quit();
  }

  
}